package com.fishcoco.app.yunhang.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.fishcoco.app.yunhang.R;
import com.fishcoco.app.yunhang.event.StartBrotherEvent;
import com.fishcoco.app.yunhang.event.StartBrotherEventForresult;
import com.fishcoco.app.yunhang.fragments.base.BaseMainFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import me.yokeyword.fragmentation.SupportFragment;

public class MainFragment extends BaseMainFragment implements BottomNavigationBar.OnTabSelectedListener {
    private final int TAB_COUNT = 3;
    private final int FIRST = 0;
    private final int SECOND = 1;
    private final int THIRD = 2;
    private int mCurrentPosition = 0;
    private final int REQ_MSG = 10;
    private final SupportFragment[] mFragments = new SupportFragment[TAB_COUNT];
    private BottomNavigationBar bottomNavigationBar;

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_main;
    }

    @Override
    protected void setUpView() {
        bottomNavigationBar = $(R.id.bottom_navigation_bar);

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.icon_teld_the_conven, "one"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_sc_smartbox, "two"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_appointment, "three"))
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
                .setMode(BottomNavigationBar.MODE_DEFAULT)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            mFragments[FIRST] = Fragment1.newInstance();
            mFragments[SECOND] = Fragment2.newInstance();
            mFragments[THIRD] = Fragment3.newInstance();

            loadMultipleRootFragment(R.id.fl_tab_container, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD]
            );
        } else {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题

            // 这里我们需要拿到mFragments的引用,也可以通过getChildFragmentManager.getFragments()自行进行判断查找(效率更高些),用下面的方法查找更方便些
            mFragments[FIRST] = findChildFragment(Fragment1.class);
            mFragments[SECOND] = findChildFragment(Fragment2.class);
            mFragments[SECOND] = findChildFragment(Fragment3.class);
        }
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (requestCode == REQ_MSG && resultCode == RESULT_OK) {

        }
    }

    @Override
    public void onTabSelected(int position) {
        if (mCurrentPosition != position) {
            showHideFragment(mFragments[position], mFragments[mCurrentPosition]);
            mCurrentPosition = position;
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    /**
     * start other BrotherFragment
     */
    @Subscribe
    public void startBrother(StartBrotherEvent event) {
        start(event.targetFragment);
    }

    /**
     * start other BrotherFragment
     */
    @Subscribe
    public void startBrotherForResult(StartBrotherEventForresult event) {
        startForResult(event.targetFragment, event.reqCode);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

}
